package com.arvinclub.web.controller.user;

import com.arvinclub.model.entity.User;
import com.arvinclub.service.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 用户关注相关
 */
@Controller
public class AttentionController {

    @Resource
    private UserService userService;

    /**
     * 关注
     */
    @PostMapping("user/addAttention/{userId}/{page}")
    public String addAttention(HttpSession session, @PathVariable int userId, @PathVariable String page) {
        User me = (User) session.getAttribute("user");
        userService.addAttention(me.getId(), userId);
        return "redirect:/user/page.html/" + userId + "/" + page;
    }

    /**
     * 取关
     */
    @PostMapping("user/delAttention/{userId}/{page}")
    public String delAttention(HttpSession session, @PathVariable int userId, @PathVariable String page) {
        User me = (User) session.getAttribute("user");
        userService.delAttention(me.getId(), userId);
        return "redirect:/user/page.html/" + userId + "/" + page;
    }

    /**
     * 关注列表
     */
    @GetMapping("user/attentionList/{userId}")
    public String attentionList(@PathVariable int userId, Model model) {
        PageInfo<User> listPage = userService.attentionList(userId);
        model.addAttribute("listPage", listPage);
        model.addAttribute("host", userService.findUserById(userId));
        model.addAttribute("msg", "的关注");
        return "list";
    }

    /**
     * 粉丝列表
     */
    @GetMapping("user/fansList/{userId}")
    public String fansList(@PathVariable int userId, Model model) {
        PageInfo<User> listPage = userService.fansList(userId);
        model.addAttribute("listPage", listPage);
        model.addAttribute("host", userService.findUserById(userId));
        model.addAttribute("msg", "的粉丝");
        return "list";
    }
}
